package sunbao.ido.experimentalField.listener;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * \* User: MeiZhongHao
 * \* Date: 2018-11-13
 * \* Time: 18:58
 * \* Description:
 * \
 */
public class SmsManager {

    private static final ConcurrentMap<String, SmsEventListener> LISTENERS;

    private static final ExecutorService CONFIG_CHANGE_EXECUTOR;

    static {
        LISTENERS = new ConcurrentHashMap();
        CONFIG_CHANGE_EXECUTOR = Executors.newFixedThreadPool(10, new ThreadFactory() {
            private AtomicInteger threadIndex = new AtomicInteger(0);
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, String.format("SmsEventListener%d", new Object[]{Integer.valueOf(this.threadIndex.incrementAndGet())}));
            }
        });
    }

    /**
     * 添加事件
     *
     * @param listener DoorListener
     */
    public static void addSmsEventListener(SmsEventListener listener) {
        if (listener != null) {
            LISTENERS.put(listener.getClass().getName(), listener);
        }
    }

    public static void addSmsEventListener(List<SmsEventListener> listeners) {
        if (listeners != null) {
            Iterator var1 = listeners.iterator();

            while (var1.hasNext()) {
                SmsEventListener listener = (SmsEventListener) var1.next();
                addSmsEventListener(listener);
            }

        }
    }

    /**
     * 移除事件
     *
     * @param listener DoorListener
     */
    public static void removeSmsEventListener(SmsEventListener listener) {
        if (listener != null) {
            LISTENERS.remove(listener.getClass().getName());
        }
    }

    /**
     * 通知所有的DoorListener
     */
    public static void notifyListeners(UserRegisterEvent event) {
        if (!SmsManager.LISTENERS.isEmpty()) {
            final CountDownLatch doneSignal = new CountDownLatch(SmsManager.LISTENERS.size());
            Iterator var12 = SmsManager.LISTENERS.values().iterator();

            while (var12.hasNext()) {
                final SmsEventListener listener = (SmsEventListener) var12.next();
                SmsManager.CONFIG_CHANGE_EXECUTOR.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            listener.sendSms(event);
                        } catch (Throwable var5) {
                            System.out.println("SmsEventListener sendSms handle error, name=" + this.getClass().getSimpleName());
                        } finally {
                            doneSignal.countDown();
                        }
                    }
                });
            }

            try {
                doneSignal.await();
            } catch (InterruptedException var9) {
                System.out.println("SmsEventListener sendSms handle  thread interrupted");
                Thread.currentThread().interrupt();
            }
        }
    }

}